<?php
class model_generalfield extends framework_base_model {
    /**
     * 获取大分类列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function get_generalfield_list($nextNum = 0,$pagesize = 0,$condition = array()) {
    	$sql = "select `product_class_id`,`class_name`,`status`,`raw_add_time`,`raw_update_time`,`author`,`editer` from `weixin_product_class` where `delete_flag` = 0 ";
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['class_name'])) {
    			$where .= " and `class_name` like '%{$condition['class_name']}%'";
    		}
    	}
    	$where .= " order by `product_class_id` desc";
    	$limit = "";
        if($pagesize != 0 ){
    		$limit = " limit {$nextNum},{$pagesize}";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql.$where.$limit);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		$sql = "select count(*) from `weixin_product_class` where `delete_flag` = 0 {$where}";
		$binds2 = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds2);
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	//获取通用价格字段
	public static function get_general_field($parent_id){
		$return = array();
		$sql1 = "select `price_attr_id`,`produce_id`,`attr_id`,`pre_price`,`cur_price` from `weixin_price_attr` where `produce_id` in (select `product_id` from `weixin_product` where `parent_id`={$parent_id} and `delete_flag`=0) and `delete_flag`=0";
		try {
			//查询该大类对应的字段
			$pdo = self::DB()->dbh;
    		$st = $pdo->prepare($sql1);
    		$st->execute();
    		$result1 = $st->fetchAll();
			if(empty($result1)){
				return $return;
			}
			$temp = array();
			$res = array();
			foreach($result1 as $value){
				$key = $value['attr_id'].$value['cur_price'];
				$res[$value['produce_id']] = 0;
				$temp[$key][] = $value;
			}
			if(empty($temp)){
				return $return;
			}
			//公用字段查找
			$len = count($res);
			$ids = array();
			$attrid = array();
			foreach($temp as $key=>$value){
				if(count($value) == $len){
					foreach($value as $v){
						$ids[] = $v['produce_id'];
						$attrid[] = $v['attr_id'];
					}
				}
			}
			//公式表查询
			array_unique($ids);
			$ids = implode(',', $ids);			
			$sql2 = "select `formula_id`,`price_list`,`small_class_id` from weixin_formula where `big_class_id` = {$parent_id} and `small_class_id` in ({$ids}) and `delete_flag` = 0";
			$st = $pdo->prepare($sql2);
    		$st->execute();
    		$result2 = $st->fetchAll();
			if(empty($result2)){
				return $return;
			}
			$temp = array();
			foreach($result2 as $value){
				if(empty($temp)){
					$temp = explode('/', $value['price_list']);
				}
				$temp = array_intersect(explode('/', $value['price_list']), $temp);
			}
			//组合价格字段ID进行查询
			if(empty($temp)){
				return $return;
			}
			
			$list = array();
			foreach($temp as $value){
				$arr = explode(',', $value);
				if(in_array($arr[count($arr)-1], $attrid)){
					$list = array_merge($list, $arr);
				}
			}
			array_unique($list);
			$list = implode(',', $list);
			$sql3 = "select `product_price_id` as id,`parent_id` as parentid,`price_name`,`depth` from `weixin_product_price`  where `delete_flag` = 0 ";
			$st = $pdo->prepare($sql3." and `product_price_id` in ({$list})");
			$st->execute();
			$return['price_list'] = $st->fetchAll();
			//数据查询
			$sql4 = "select `price_attr_id`,`produce_id`,`attr_id`,`pre_price`,`cur_price` from `weixin_price_attr` where `produce_id` in (select `product_id` from `weixin_product` where `parent_id`={$parent_id} and `delete_flag`=0) and `produce_id` in ({$ids}) and `delete_flag`=0";
			$st = $pdo->prepare($sql4);
			$st->execute();
			$result4 = $st->fetchAll();
			foreach($result4 as $key=>$value){
				$return['modify'][$value['attr_id']][] = $value['produce_id'];
				$return['price_data'][$key]['attr_id'] = $value['attr_id'];
				$return['price_data'][$key][] = array('range'=>array($value['cur_price'],$value['cur_price'],$value['cur_price']-$value['pre_price'],$value['price_attr_id']));
			}
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
		return $return;
	}
}
